package com.liam.socket.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class Client {

    public static void main(String[] args) throws IOException {
        // 创建客户端
        DatagramSocket socket = new DatagramSocket();

        // 创建集装箱
        byte [] bytes = "客户端：呵呵".getBytes();
        // 创建集装箱
        DatagramPacket packet = new DatagramPacket(bytes,bytes.length);
        packet.setSocketAddress(new InetSocketAddress("127.0.0.1",61610));

        // 发送数据
        socket.send(packet);


        // 创建集装箱
        byte [] bs = new byte[1024];
        DatagramPacket res = new DatagramPacket(bs,bs.length);
        //接受服务端数据
        socket.receive(res);
        System.out.println(new String(res.getData(),0,res.getLength()));


        // 关闭资源
        socket.close();
    }
}
